<?php


namespace App\Http\Controllers;


use App\Models\Article;
use App\Server\AliOSS;
use Illuminate\Http\Request;

class ArticleController extends Controller
{
    /**
     * 列表
     * @return \Illuminate\Http\JsonResponse
     */
    public function index()
    {
        $data = Article::where('status', 1)
            ->where('release_time', '<=', date('Y-m-d H:i:s'))
            ->select(['id', 'title', 'img', 'release_time'])
            ->orderByDesc('release_time')
            ->paginate(10);
        foreach ($data as &$item) {
            $item->img = img_url(AliOSS::getThumbName($item->img));
        }
        return $this->success($data);
    }


    /**
     * 详情
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \Throwable
     */
    public function details(Request $request)
    {
        throw_err(!$request->id, 'id不能为空');
        $data = Article::where('id', $request->id)->select(['id', 'title', 'content', 'release_time'])->first();
        $data->read_num += 1;
        $data->save();
        return $this->success($data);
    }
}